﻿''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class listeChampsSortieTableIntegrationGrilleTarifaire
    Inherits baseListeChampsSortieTable

#Region "Constantes sur les noms des champs de la grille tarifaire"
    Protected nomPComDC_Non_Derog As String = "PComDC_Non_Derog"
    Protected nomPourDC_Derog As String = "PourDC_Derog"
    Protected nomPComDC_Derog As String = "PComDC_Derog"
    Protected nomPComINCINV_Non_Derog As String = "PComINCINV_Non_Derog"
    Protected nomPourINCINV_Derog As String = "PourINCINV_derog"
    Protected nomPComINCINV_Derog As String = "PComINCINV_derog"

    Protected descPComDC_Non_Derog As String = "Prime commercial DC non dérogée"
    Protected descPourDC_Derog As String = "Pourcentage de dérogations DC"
    Protected descPComDC_Derog As String = "Prime commercial DC dérogée moyenne"
    Protected descPComINCINV_Non_Derog As String = "Prime commercial INCINV non dérogée"
    Protected descPourINCINV_Derog As String = "Pourcentage de dérogations INCINV"
    Protected descPComINCINV_Derog As String = "Prime commercial INCINV dérogée"
#End Region

    ''' <summary>
    ''' Initialisation de la liste des champs attendus en sortie de la grille tarifaire
    ''' </summary>
    ''' <remarks></remarks>
    Public Overrides Function initialise(ByVal sender As Object) As Boolean
        Add(descPComDC_Non_Derog, New Correspondance(sender, _
                                                     descPComDC_Non_Derog, _
                                                     New ChampsBDD(nomPComDC_Non_Derog, _
                                                                   New FormatChamps(GetType(Double))), _
                                                     typeChEntreeMapping.TABLE))
        Add(descPComDC_Derog, New Correspondance(sender, _
                                                 descPComDC_Derog, _
                                                 New ChampsBDD(nomPComDC_Derog, _
                                                               New FormatChamps(GetType(Double))), _
                                                 typeChEntreeMapping.TABLE))
        Add(descPourDC_Derog, New Correspondance(sender, _
                                                 descPourDC_Derog, _
                                                 New ChampsBDD(nomPourDC_Derog, _
                                                               New FormatChamps(GetType(Double))), _
                                                 typeChEntreeMapping.TABLE))
        Add(descPComINCINV_Non_Derog, New Correspondance(sender, _
                                                         descPComINCINV_Non_Derog, _
                                                         New ChampsBDD(nomPComINCINV_Non_Derog, _
                                                                       New FormatChamps(GetType(Double))), _
                                                         typeChEntreeMapping.TABLE))
        Add(descPComINCINV_Derog, New Correspondance(sender, _
                                                     descPComINCINV_Derog, _
                                                     New ChampsBDD(nomPComINCINV_Derog, _
                                                                   New FormatChamps(GetType(Double))), _
                                                     typeChEntreeMapping.TABLE))
        Add(descPourINCINV_Derog, New Correspondance(sender, _
                                                     descPourINCINV_Derog, _
                                                     New ChampsBDD(nomPourINCINV_Derog, _
                                                                   New FormatChamps(GetType(Double))), _
                                                     typeChEntreeMapping.TABLE))
        _initHasBeenMade = True
        AddHandler CType(sender, tableBDD).ListeChampsEntreeChanged, AddressOf onTraitement_ListeChampsEntreeChanged
        Return True
    End Function

    Protected Overridable Sub onTraitement_ListeChampsEntreeChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim traitementTableBDD As tableBDD = DirectCast(sender, tableBDD)
        If verifContraintesGrilleTarifaire(traitementTableBDD) Then

            Dim nouvelleList As New List(Of Correspondance)

            For Each corresp As Correspondance In traitementTableBDD.ListeChampsEntree.Values
                Dim estDejaPresente As Boolean = False
                Dim correspPresente As Correspondance = Nothing
                For Each correspSortie As Correspondance In Values
                    If correspSortie.chOutil.Nom = corresp.chOutil.Nom Then
                        estDejaPresente = True
                        correspPresente = correspSortie
                    End If
                Next
                If estDejaPresente Then
                    nouvelleList.Add(correspPresente)
                Else
                    nouvelleList.Add(New Correspondance(traitementTableBDD, _
                                                        corresp.description, _
                                                        corresp.chBDD, _
                                                        typeChEntreeMapping.TABLE))
                End If
            Next

            Clear()
            For Each corresp As Correspondance In nouvelleList
                Add(corresp.description, corresp)
            Next
        End If
    End Sub

    ''' <summary>
    ''' Validation que la table en entrée vérifie bien les contraintes d'une table de grille tarif
    ''' </summary>
    ''' <remarks></remarks>
    Private Function verifContraintesGrilleTarifaire(ByVal tableBDDrattachement As tableBDD) As Boolean
        Dim result As Boolean = True
        Dim listeChampsObligEntree As String() = New String() {nomPComDC_Non_Derog, nomPComDC_Derog, _
                                                               nomPourDC_Derog, nomPComINCINV_Non_Derog, _
                                                               nomPComINCINV_Derog, nomPourINCINV_Derog}
        For Each nomChamps As String In listeChampsObligEntree
            If Not tableBDDrattachement.ListeChampsEntree.ContainsKey(nomChamps) Then
                MsgBox("La grille tarifaire " & tableBDDrattachement.FichierBDD.NomComplet & " chargée ne contient pas le champs " & nomChamps)
                result = False
            End If
        Next
        Return result
    End Function

End Class

